import requests
import urllib.request as req
from urllib import error
import json
import os
import pandas as pd
import math
import time
from random import randint
import csv


# user-agent池
USER_AGENTS = [
 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
 "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
 "Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
 "Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
 "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
 "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
 "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
 "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
 "Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
 "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
 "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
 "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
 "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
 "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52"
]

# api_key池
API_KEY = [
    "1e5deaaa74515816880b36bf28c5a569",
    "fd58a5df21d2e87f9c99e10adfba1f1f",
    "302bafaa6ecda254166e0bec2d79ba12",
    "d9767a0692bdfc4db28507b950196d4c",
    "24f54c0d8f551f17bb68d92141c7de32",
    "50c21d685deb200bbc04b526698f2664",
    "06640f9d5e4e22df7008592cfe25e276",
    "8a0c01e477e76a9ca1133464555002f0",
    "dad0eac0ba4fd47ec6635c50e42f3b50",
    "dad0eac0ba4fd47ec6635c50e42f3b50",
    "289ceecb87390346d78acfd9d83d49d1",
    "4f3b149c0a5353fa2c8d8359092c58ec",
    "55f248d69cf3846aac2ac6c869f85fb4",
    "ec4f906c91dcc86b4dae568465dd0259"
]
per_page = 100

#获取已经存在本地的包名称
def get_pkg_name():
    filename = 'packagename.csv'
    data = []
    with open(filename) as csvfile:
        csv_reader = csv.reader(csvfile)  # 使用csv.reader读取csvfile中的文件
        # header = next(csv_reader)        # 读取第一行每一列的标题
        for row in csv_reader:  # 将csv 文件中的数据保存到data中
            data.append(row[0])  # 选择某一列加入到data数组中
    return data



#获取仓库commits信息
def get_repo_commits(pkg_name):
    # 构建API请求
    api_key = API_KEY[randint(0, len(API_KEY) - 1)]
    url = "https://gitee.com/api/v5/repos/src-openEuler/%s/commits?access_token=e99cbed3f6058fb83c083484afe724e9&api_key=%s"% (pkg_name, api_key)
    random_agent = USER_AGENTS[randint(0, len(USER_AGENTS) - 1)]
    headers = {'User-Agent': random_agent}
    response = requests.get(url, headers=headers)
    response_dict = response.json()
    print("successfully get %s commits !\n" % pkg_name)
    return response_dict

#获取仓库详细信息
# https://gitee.com/api/v5/repos/src-openEuler/libtiff/contributors   获取贡献者的信息
def get_repo_detail(pkg_name):
    # 构建API请求
    api_key = API_KEY[randint(0, len(API_KEY) - 1)]
    url = "https://gitee.com/api/v5/repos/src-openEuler/%s?access_token=e99cbed3f6058fb83c083484afe724e9&api_key=%s"% (pkg_name, api_key)
    random_agent = USER_AGENTS[randint(0, len(USER_AGENTS) - 1)]
    headers = {'User-Agent': random_agent}
    response = requests.get(url, headers=headers)
    response_dict = response.json()
    #print(response_dict["stargazers_count"])
    print("successfully get %s detail !\n" % pkg_name)
    return response_dict

#获取仓库issues
def get_repo_issues(pkg_name):
    # 构建API请求
    api_key = API_KEY[randint(0, len(API_KEY) - 1)]
    #url = "https://gitee.com/api/v5/repos/src-openEuler/%s/issues?access_token=e99cbed3f6058fb83c083484afe724e9&api_key=%s"% (pkg_name, api_key)
    url ="https://gitee.com/api/v5/repos/src-openEuler/%s/issues?access_token=86a93c7f509f3911ee0bace0ee49ba86&state=all&sort=created&direction=desc&page=1&per_page=100&api_key=%s"% (pkg_name, api_key)
    random_agent = USER_AGENTS[randint(0, len(USER_AGENTS) - 1)]
    headers = {'User-Agent': random_agent}
    response = requests.get(url, headers=headers)
    response_dict = response.json()
    print("successfully get %s issues !\n" % pkg_name)
    return response_dict




if __name__ == '__main__':
    pkg_list = get_pkg_name()

    # 获取信息并存储
    for i in range(len(pkg_list)):
        commits = get_repo_commits(pkg_list[i])
        detail = get_repo_detail(pkg_list[i])
        issues = get_repo_issues(pkg_list[i])
        filename1 = 'commits/'+ pkg_list[i] +'_commits'+'.json'
        filename2 = 'detail/'+ pkg_list[i] + '_detail' + '.json'
        filename3 = 'issues/'+ pkg_list[i] + '_issues' + '.json'
        with open(filename1, 'w') as file_obj1:
            json.dump(commits, file_obj1)
        with open(filename2, 'w') as file_obj2:
            json.dump(detail, file_obj2)
        with open(filename3, 'w') as file_obj3:
            json.dump(issues, file_obj3)
        #time.sleep(1)
        print("Saved successfully" + pkg_list[i])
